﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using HAF.FoundationLib;
using Newtonsoft.Json;
using WareDealer.Helper;
using WareDealer.Mode;

namespace WareDealer.Works
{
    /// <summary>
    /// 广告采集任务
    /// </summary>
    [DisplayName("促销采集工作项")]
    public class JdSubHeadWork : JdWorkItem
    {
        /// <summary>
        /// 执行函数
        /// </summary>
        public override void ExecItem()
        {
            List<ProductInfo> wares = WorkItems as List<ProductInfo>;
            if (wares == null || wares.Count <= 0) return;
            Statu = EnumGatherStatu.Begin;

            List<string> wList = wares.Select(item => item.ProductID).ToList();
            GetWareAdMessage(wList);

        }
        [DisplayName("获取促销信息")]
        private void GetWareAdMessage(List<string> pids)
        {
            try
            {
                Faster.XLogInfo(string.Format("批量采集促销{0}更新开始", Name));
                if (pids != null && pids.Count > 0)
                {
                    string skuids = string.Join(",AD_", pids.ToArray());
                    string url = string.Format("http://ad.3.cn/ads/mgets?skuids={0}&_={1}", skuids, Golemer.GetTimeStamp());
                    string strCoupon = HttpHelper.GetResponseUTF(url, "get", string.Empty);
                    List<AdMessageTag> myCoupons = JsonConvert.DeserializeObject<List<AdMessageTag>>(strCoupon);
                    if (myCoupons != null && myCoupons.Count > 0)
                    {
                        foreach (var item in myCoupons)
                        {
                            item.ad = Golemer.ClearHtml(item.ad);
                            item.id = item.id.Replace("AD_", "");
                        }
                        ResultItems = myCoupons;
                    }
                }
                Statu = EnumGatherStatu.End;
                Faster.XLogInfo(string.Format("批量采集促销{0}更新结束", Name));
            }
            catch (Exception ex)
            {
                Statu = EnumGatherStatu.End;
                Faster.XLogErr(ex.Message);
            }

        }
    }
}
